import { sampleSize } from 'lodash-es';
import request from '@/utils/request';
import selectAudio from '@/assets/audio/select.mp3';
import okAudio from '@/assets/audio/ok.mp3';
import endAudio from '@/assets/audio/end.mp3';

export function useGame(frontArea, backArea, playMethods, type) {
  // const Toast = inject('toast');
  const curentIndex = ref(0); // 默认第一个玩法
  const fontSelect = ref([]); // 前区选中的数据
  const backSelect = ref([]); // 后区选中的数据
  const issueNum = ref(''); // 期号
  const prize = ref(0); // 奖池金额
  let timer = null, timer2 = null, myAudio = null;
  const curentPm = computed(() => playMethods[curentIndex.value]); // 当前玩法信息
  const lastPmIndex = playMethods.length - 1; // 数组下标从0开始所以减一

  function transPoolAmt(amt) {
    if (amt >= 100000000) {
      const hm = (amt / 100000000); // 亿 （hundred million）
      return `${Math.floor(hm * 100) / 100}亿`;
    } else if (amt >= 10000) {
      const tt = (amt / 10000); // 万（ten thousand）
      return `${Math.floor(tt * 100) / 100}万`;
    } else {
      return amt;
    }
  }

  function getRandomRes() {
    const { frontNum, backNum } = curentPm.value;
    fontSelect.value = sampleSize(frontArea, frontNum);
    backSelect.value = sampleSize(backArea, backNum);
  }

  function startHandle(duration = 2000) {
    if (timer2) clearTimeout(timer2)
    if (timer) clearInterval(timer)
    getRandomRes();
    timer = setInterval(getRandomRes, duration);
  }

  function okHandle() {
    // 开始摇号
    startHandle(200);
    timer2 = setTimeout(() => {
      clearInterval(timer)
      timer2 = null
      playBtnMusic(endAudio)
    }, 2000)
  }

  function changePm(i) {
    curentIndex.value = i;
    playBtnMusic(selectAudio);
    startHandle();
  }

  function handleMessage(event) {
    // console.log(2222, event.data);
    switch (event.data) {
      case "left":
        playBtnMusic(selectAudio);
        curentIndex.value = (curentIndex.value == 0) ? lastPmIndex : curentIndex.value - 1;
        startHandle();
        break;
      case "right":
        playBtnMusic(selectAudio);
        curentIndex.value = (curentIndex.value == lastPmIndex) ? 0 : curentIndex.value + 1;
        startHandle();
        break;
      case "ok":
        playBtnMusic(okAudio);
        okHandle();
        break;
    }
  }

  function playBtnMusic(src) {
    myAudio.src = src;
    myAudio.play();
  }

  onBeforeMount(() => {
    // Toast({ message: "22222222" });
    myAudio = new Audio();
    myAudio.autoplay = true;
    // 开始动画
    startHandle();
    // 监听 message 接收用户按键信息
    window.addEventListener('message', handleMessage);
    // 获取奖池信息
    request.get("lottery/pool", { params: { type } }).then(res => {
      const { pool, term } = res
      issueNum.value = term;
      prize.value = transPoolAmt(pool);
    });
  })

  onBeforeUnmount(() => {
    myAudio = null;
    if (timer) clearInterval(timer);
    if (timer2) clearTimeout(timer2)
    window.removeEventListener('message', handleMessage);
  })

  return { fontSelect, backSelect, curentPm, issueNum, prize, changePm, okHandle };
}
